﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate;
using SimpleScrum.Core.Entities;
using SimpleScrum.Helpers;
using WebMatrix.WebData;

namespace SimpleScrum.Areas.Scrum.Models
{
    public class UsersModel
    {
        private ISession _session;

        public Guid GetUsersIdByEmail()
        {
            ISession session;
            Guid id = Guid.Empty;
            using (session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query = session.CreateQuery("from Users where Email='" + WebSecurity.CurrentUserName + "';");
                Users firstOrDefault = query.List<Users>().FirstOrDefault();
                if (firstOrDefault != null) id = firstOrDefault.Id;
            }
            return id;
        }

        public IList<Users> GetUsersList(Guid id)
        {
            IList<Users> users;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from Users where ID" + id + "';");
                users = query.List<Users>();
            }
            return users;
        }

        public IList<Users> GetUsersList()
        {
            IList<Users> users;
            using (_session = NHibernateHelper.GetSession())
            {
                //NHibernate query 
                IQuery query =
                    _session.CreateQuery("from Users;");
                users = query.List<Users>();
            }
            return users;
        }

        public Users GetUsersById(Guid id)
        {
            Users users;
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    users = _session.Get<Users>(id);
                    tran.Commit();
                }
            }
            return users;
        }

        public bool NewUsers(Users users)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Save(users);
                    tran.Commit();
                }
            }
            return true;
        }

        public void UpdateUsers(Users users)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    _session.Update(users);
                    tran.Commit();
                }
            }
        }

        public void DeleteUsers(Users users)
        {
            using (_session = NHibernateHelper.GetSession())
            {
                using (ITransaction tran = _session.BeginTransaction())
                {
                    try
                    {
                        _session.Delete(users);
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                    }
                }
            }
        }
    }
}